<?php

namespace app\controller;

use app\BaseController;
use app\model\FortifyModel;
use think\facade\Db;
use think\facade\View;

class Index extends BaseController
{
    public function task()
    {
        //查询需要扫描的仓库
        $id = $_GET['id'];

        $mtime = date("Y-m-d H:i:s", strtotime("-1 hour"));
        $list = Db::table('code')
            ->where(['id' => $id])
//            ->whereTime('time', '<', $mtime)
            ->select()
            ->toArray();

        //遍历仓库信息($list as $codeInfo){

        foreach ($list as $x => $y) {
//                var_dump($y["url"]);
            $project_name = $y['name'];

            //1. 拉取代码
            $codePath = '/root/file';
            if (file_exists("/root/file/{$project_name}") == false) {

                $cmd = "cd {$codePath}/ && git clone --depth=1 {$y["url"]}  {$project_name}";
                system($cmd);
            }

//               var_dump($cmd);


            //2. 扫描代码

            $out = "/root/file/{$project_name}";
            FortifyModel::startScan($codePath, $out);

            //4. 存储结果
            FortifyModel::chenting($y['id'], $out . ".xml");

            //5. 更新时间

            $time = date('Y-m-d H:i:s', time()); // 2018-10-3 15:57:05
            $all3 = Db::table('code')
                ->update(['id' => $y['id'], 'time' => $time]);
        }

//        echo "休息10秒..." . PHP_EOL;
        return redirect(url('index/index'));


    }

    public function index()
    {
        $list = Db::table('code')
            ->select()
            ->toArray();
        $data['list'] = $list;
        return View::fetch('index/index', $data);


    }

    public function suibian()
    {

        //查询需要扫描的仓库

        $mtime = date("Y-m-d H:i:s", strtotime("-1 hour"));
//            var_dump($mtime);
        $list = Db::table('suibian')
            ->where(['cid' => $_GET['cid']])
            ->limit(100)
            ->order('id desc')
            ->select()
            ->toArray();
        $data['list'] = $list;
        return View::fetch('index/suibian', $data);


    }

    public function code_check()
    {

        //查询需要扫描的仓库
        $id = $_GET['id'];
        $info = Db::table('suibian')->where(['id' => $id])->find();
//        var_dump($info);die;
        $data['info'] = $info;
        return View::fetch('index/code_check', $data);


    }

    public function add()
    {

        return View::fetch('index/add');


    }

    public function _add()
    {
        Db::name('code')->insert($_POST);
        return redirect(url('index/index'));
    }


}


